<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Agent;


use App\Dao\AgentMoneyDao;
use App\Dao\AgentDao;
use App\Dao\BusinessDao;
use App\Dao\UserDao;
use App\Service\Agent\MoneyService;
use App\Kernel\Util\AgentJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 收益服务
 *
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class ProfitService extends Service
{

    /**
     * @Inject
     * @var MoneyService
     */
    public $money_service;

    /**
     * @Inject
     * @var AgentDao
     */
    public $agent_dao;

    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * 单条商家流水详情
     * @param int $store_id
     * @return mixed
     */
    public function getStoreProfitById(int $store_id)
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'business_id' => $store_id
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $business = $this->business_dao->first(['id' => $value['business_id']],['name','legal_name','phone','tel']);
                $data['list'][$key]['store_name'] = $business['name'];
                $data['list'][$key]['legal_name'] = $business['legal_name'];
                $data['list'][$key]['phone'] = $business['phone'];
                $data['list'][$key]['tel'] = $business['tel'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }
        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'business_id' => $store_id
        ]);
        $data['total_commission'] = $this->money_service->getMoneyBalanceByStore(agentId());
        return $data;
    }

    /**
     * 收益管理
     */
    public function listTop()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $month_store_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
            'price_source' => 1,
        ],$time_start,$time_end);

        $month_channel_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
            'price_source' => 2,
        ],$time_start,$time_end);

        $month_store_count = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
            'price_source' => 1,
        ],$time_start,$time_end);


        $month_channel_count = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
            'price_source' => 2,
        ],$time_start,$time_end);

        $data['total_revenue'] = $this->money_service->getMoneyBalanceByAgent(agentId());
        $data['channel_revenue'] = $this->money_service->getMoneyBalanceByCommission(agentId());
        $data['store_revenue'] = $this->money_service->getMoneyBalanceByStore(agentId());
        $data['month_channel_count'] = $month_store_money ?? 0.00;
        $data['month_channel_money'] = $month_store_count ?? 0;
        $data['month_store_count'] = $month_channel_money ?? 0.00;
        $data['month_store_money'] = $month_channel_count ?? 0;
        $data['month_total_money'] = filterMoney($month_store_money + $month_channel_money, 2);

        return $data;
    }

    /**
     * 代理商家流水 - 昨天
     */
    public function businessProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 1,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $business = $this->business_dao->first(['id' => $value['business_id']],['name','legal_name','phone','tel']);
                $data['list'][$key]['store_name'] = $business['name'];
                $data['list'][$key]['legal_name'] = $business['legal_name'];
                $data['list'][$key]['phone'] = $business['phone'];
                $data['list'][$key]['tel'] = $business['tel'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }
        $data['total'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 1,
        ],$time_start,$time_end);

        return $data;
    }

    /**
     * 代理渠道商流水 - 昨天
     *
     */
    public function channelProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 2,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }
        $data['total'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 2,
        ],$time_start,$time_end);

        return $data;
    }

    /**
     * 代理商家流水 - 全部
     */
    public function businessProfitList()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 1,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $business = $this->business_dao->first(['id' => $value['business_id']],['name','legal_name','phone','tel']);
                $data['list'][$key]['store_name'] = $business['name'];
                $data['list'][$key]['legal_name'] = $business['legal_name'];
                $data['list'][$key]['phone'] = $business['phone'];
                $data['list'][$key]['tel'] = $business['tel'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }
        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 1,
        ]);
        $data['total_commission'] = $this->money_service->getMoneyBalanceByStore(agentId());
        return $data;
    }

    /**
     *  代理渠道商流水 - 全部
     */
    public function channelProfitList()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 2,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }
        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'price_source' => 2,
        ]);
        $data['total_commission'] = $this->money_service->getMoneyBalanceByCommission(agentId());
        return $data;
    }

    /**
     * 分公司收益
     */
    public function companyProfitTop()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $month_area_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ],$time_start,$time_end);

        $month_city_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ],$time_start,$time_end);

        $month_partner_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
        ],$time_start,$time_end);

        $month_channel_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ],$time_start,$time_end);

        $total_area_money = $this->money_service->agent_money_dao->sumMoneyBalance([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ]);

        $total_city_money = $this->money_service->agent_money_dao->sumMoneyBalance([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ]);

        $total_partner_money = $this->money_service->agent_money_dao->sumMoneyBalance([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 2,
        ]);

        $data['total_commission'] = $this->money_service->getMoneyBalanceByAgent(agentId());
        $data['channel_total_commission'] = $this->money_service->getMoneyBalanceByCommission(agentId());
        $data['area_total_commission'] = $total_area_money;
        $data['city_total_commission'] = $total_city_money;
        $data['partner_total_commission'] = $total_partner_money;

        $data['yesterday_total_commission'] = filterMoney($month_channel_money + $month_area_money + $month_city_money + $month_partner_money, 2);
        $data['yesterday_channel_total_commission'] = $month_channel_money ?? 0.0;
        $data['yesterday_area_total_commission'] = $month_area_money ?? 0.0;
        $data['yesterday_city_total_commission'] = $month_city_money ?? 0.0;
        $data['yesterday_partner_total_commission'] = $month_partner_money ?? 0.0;

        return $data;
    }

    /**
     * 昨日分公司市代返佣
     */
    public function yesterdayCityProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ]);

        return $data;
    }

    /**
     * 昨日分公司区代返佣
     */
    public function yesterdayAreaProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ]);

        return $data;
    }

    /**
     * 昨日分公司渠道商返佣
     */
    public function yesterdayChannelProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['area'] = $user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
                $data['list'][$key]['order_no'] = $value['order_no'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ]);

        return $data;
    }

    /**
     *昨日分公司合伙人市代返佣
     */
    public function yesterdayPartnerCityProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 4,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 4,
        ],$time_start,$time_end);

        return $data;
    }

    /**
     * 昨日分公司合伙人区代返佣
     */
    public function yesterdayPartnerAreaProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 3,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 3,
        ],$time_start,$time_end);

        return $data;
    }

    /**
     * 昨日分公司合伙人渠道商返佣
     */
    public function yesterdayPartnerChannelProfit()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $result = $this->money_service->agent_money_dao->money([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 2,
        ],$time_start,$time_end);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['area'] = $user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
                $data['list'][$key]['order_no'] = $value['order_no'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->monthCount([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 2,
        ],$time_start,$time_end);

        return $data;
    }

    /**
     * 分公司直招市代流水列表
     */
    public function getOfficeCityList()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ]);

        return $data;
    }

    /**
     * 分公司直招区代流水列表
     */
    public function getOfficeAreaList()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ]);

        return $data;
    }

    /**
     * 分公司直招渠道商流水列表
     */
    public function getOfficeChannelList()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['area'] = $user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
                $data['list'][$key]['order_no'] = $value['order_no'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ]);

        return $data;
    }

    /**
     * 分公司直招合伙人区代流水
     */
    public function getOfficePartnerArea()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 3,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $partner = $this->agent_dao->first($value['partner_id'],false);
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $data['list'][$key]['partner_name'] = $partner['name'] ?? "";
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['area'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 3,
        ]);

        return $data;
    }

    /**
     * 分公司直招合伙人市代流水
     */
    public function getOfficePartnerCity()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 4,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $partner = $this->agent_dao->first($value['partner_id'],false);
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date('Y.m.d', $value['created_at']);
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['partner_name'] = $partner['name'] ?? "";
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['area'] = $user['province'].$user['city'];
                $data['list'][$key]['city'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 4,
        ]);

        return $data;
    }

    /**
     * 分公司直招合伙人渠道商流水
     */
    public function getOfficePartnerChannel()
    {
        $result = $this->money_service->agent_money_dao->moneyAll([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 2,
        ]);

        $data = [];
        $data['list'] = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $partner = $this->agent_dao->first($value['partner_id'],false);
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['created_at'] = date("Y.m.d", $value['created_at']);
                $data['list'][$key]['partner_name'] = $partner['name'] ?? "";
                $user = $this->user_dao->first($value['lower_id']);
                $data['list'][$key]['name'] = $user['name'];
                $data['list'][$key]['phone'] = $user['phone'];
                $data['list'][$key]['address'] = $user['province'].$user['city'].$user['district'];
                $data['list'][$key]['area'] = $user['district'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['num'] = $value['num'];
                $data['list'][$key]['order_no'] = $value['order_no'];
            }
        }

        $data['total'] = $this->money_service->agent_money_dao->count([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 3,
            'price_source' => 2,
        ]);

        return $data;
    }

    /**
     * 合伙人收益
     */
    public function partnerProfitTop()
    {
        $time_start = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
        $time_end = strtotime(date('Y-m-d'.'00:00:00',time()));

        $month_area_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ],$time_start,$time_end);

        $month_city_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ],$time_start,$time_end);

        $month_channel_money = $this->money_service->agent_money_dao->monthMoney([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ],$time_start,$time_end);

        $total_area_money = $this->money_service->agent_money_dao->sumMoneyBalance([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 1,
        ]);

        $total_city_money = $this->money_service->agent_money_dao->sumMoneyBalance([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 2,
        ]);

        $total_channel_money = $this->money_service->agent_money_dao->sumMoneyBalance([
            'agent_id' => agentId(),
            'type' => 1,
            'source' => 4,
        ]);

        $data['total_commission'] = $this->money_service->getMoneyBalanceByAgent(agentId());
        $data['channel_total_commission'] = $this->money_service->getMoneyBalanceByCommission(agentId());
        $data['area_total_commission'] = $total_area_money;
        $data['city_total_commission'] = $total_city_money;
        $data['partner_total_commission'] = $total_channel_money;

        $data['yesterday_total_commission'] = filterMoney($month_channel_money + $month_area_money + $month_city_money , 2);
        $data['yesterday_channel_total_commission'] = $month_channel_money ?? 0.0;
        $data['yesterday_area_total_commission'] = $month_area_money ?? 0.0;
        $data['yesterday_city_total_commission'] = $month_city_money ?? 0.0;


        return $data;
    }
}